package org.htzb.news.dao.impl;

import org.htzb.news.dao.C3P0Util;
import org.htzb.news.dao.CommentDao;
import org.htzb.news.dao.NewsDao;
import org.htzb.news.entity.News;
import org.htzb.news.entity.Comment;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NewDaoImpl extends C3P0Util implements NewsDao {
    @Override
    public List<News> getNews(int tid) throws SQLException {
        List<News> newsList = new ArrayList<>();
        Connection connection=getConnection();//获取数据连接
        String sql="select * from news ";
        if(tid>0){
            sql+=" where NTID="+tid;
        }
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();//执行查询语句
        while(resultSet.next()) {
            int nid = resultSet.getInt("NID");
            int ntid = resultSet.getInt("NTID");
            String ntitle = resultSet.getString("NTITLE");
            String nauthor = resultSet.getString("NAUTHOR");
            Timestamp ncreateDate= resultSet.getTimestamp("NCREATEDATE");
            String npicpath = resultSet.getString("NPICPATH");
            String ncontent = resultSet.getString("NCONTENT");
            Timestamp nmodifydDate= resultSet.getTimestamp("NMODIFYDATE");
            String nsummary = resultSet.getString("NSUMMARY");


            News news=new News();
            news.setNid(nid);
            news.setNtid(ntid);
            news.setNtitle(ntitle);
            news.setNauthor(nauthor);
            news.setNpicpath(npicpath);
            news.setNcontent(ncontent);
            news.setNsummary(nsummary);
            newsList.add(news);

            System.out.println("该新闻的标题为：" + ntitle);
            System.out.println("该新闻的作者为：" + nauthor);
            System.out.println("该新闻的内容为：" + ncontent);
            System.out.println("该新闻的摘要为：" + nsummary);



        }
        return newsList;
    }

    @Override
    public News getNewsById(int id) throws SQLException {
        Connection connection=getConnection();//获取数据连接
        String sql = "SELECT * from news where NID="+id;
        //查询语句
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        News news=null;
        if(resultSet.next()) {
            news=new News();
            int nid = resultSet.getInt("NID");
            int ntid = resultSet.getInt("NTID");
            String ntitle = resultSet.getString("NTITLE");
            String nauthor = resultSet.getString("NAUTHOR");
            Timestamp ncreateDate= resultSet.getTimestamp("NCREATEDATE");
            String npicpath = resultSet.getString("NPICPATH");
            String ncontent = resultSet.getString("NCONTENT");
            Timestamp nmodifydDate= resultSet.getTimestamp("NMODIFYDATE");
            String nsummary = resultSet.getString("NSUMMARY");

            CommentDao commentDao=new CommentDaoImpl();
            List<Comment> commentList=commentDao.getCommentsByNid(id);


            news.setNid(nid);
            news.setNtid(ntid);
            news.setNtitle(ntitle);
            news.setNauthor(nauthor);
            news.setNpicpath(npicpath);
            news.setNcontent(ncontent);
            news.setNsummary(nsummary);
            news.setComments(commentList);
        }
        return news;
    }
    public static void main(String[] args) throws SQLException {
        NewsDao newsDao = new NewDaoImpl();
        News news = newsDao.getNewsById(170);
        System.out.println(news);
    }
}